﻿

CREATE PROCEDURE [dbo].[exbug_StergeUnitate] 
 @CodUnitate smallint AS
BEGIN
DECLARE @NrDoc AS int
SET @NrDoc = 0

BEGIN TRAN
    
PRINT '[AngajamentLegalDetaliu]'    
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AngajamentLegalDetaliu]') AND type in (N'U'))
	DELETE FROM AngajamentLegalDetaliu WHERE IDAL IN (SELECT IDAL FROM AngajamentLegal WHERE [CodUnitate]=@CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 

PRINT '[AngajamentLegal]'    
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AngajamentLegal]') AND type in (N'U'))
	DELETE FROM AngajamentLegal WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

PRINT '[AngajamentBugetarDetaliu]'    
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AngajamentBugetarDetaliu]') AND type in (N'U'))
	DELETE FROM AngajamentBugetarDetaliu WHERE IDAB IN (SELECT IDAB FROM AngajamentBugetar WHERE [CodUnitate]=@CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 

PRINT '[AngajamentBugetarNC]'    
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AngajamentBugetarNC]') AND type in (N'U'))
	DELETE FROM AngajamentBugetarNC WHERE IDAB IN (SELECT IDAB FROM AngajamentBugetar WHERE [CodUnitate]=@CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 

PRINT '[AngajamentBugetar]'    
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AngajamentBugetar]') AND type in (N'U'))
DELETE FROM AngajamentBugetar WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

PRINT 'Ordonantari'    
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Ordonantari]') AND type in (N'U'))
	DELETE FROM Ordonantari WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

PRINT 'arhOrdonantari'    
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[arhOrdonantari]') AND type in (N'U'))
	DELETE FROM arhOrdonantari WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 


PRINT 'PropunereAngajareCheltuialaDetaliu'    
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PropunereAngajareCheltuialaDetaliu]') AND type in (N'U'))
	DELETE FROM PropunereAngajareCheltuialaDetaliu WHERE IDPac IN (SELECT IDPac FROM PropunereAngajareCheltuiala WHERE [CodUnitate]=@CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 

PRINT '[PropunereAngajareCheltuiala]'    
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PropunereAngajareCheltuiala]') AND type in (N'U'))
	DELETE FROM [PropunereAngajareCheltuiala] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

PRINT 'ArticoleAliniate'    
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ArticoleAliniate]') AND type in (N'U'))
	DELETE FROM ArticoleAliniate WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

PRINT 'arhArticoleAliniate'    
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[arhArticoleAliniate]') AND type in (N'U'))
	DELETE FROM arhArticoleAliniate WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 


PRINT 'ResponsabilVizaCFP'
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ResponsabilVizaCFP]') AND type in (N'U'))
	DELETE FROM [ResponsabilVizaCFP] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 
    
COMMIT TRAN    
GOTO Gata
Eroare:
  ROLLBACK
  RAISERROR ('Eroare la executia procedurii StergeUnitate',-1,0)
Exista_Documente:
  ROLLBACK
  RAISERROR ('Unitatea nu poate fi stearsa deoarece contine documente!',-1,0)
Exista_Rulaje_Documente:
  ROLLBACK
  RAISERROR ('Unitatea nu poate fi stearsa deoarece contine rulaje - documente!',-1,0)

Gata:

END